package com.ttj.controller.purchaser;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ttj.domain.oracle.OrderInstock;
import com.ttj.service.PickGoodService;
import com.ttj.vo.BaseRes;

@Controller
@RequestMapping("purchaser/instock")
public class OrderInstockController {

	private Logger logger = LoggerFactory.getLogger(OrderInstockController.class);

	@Resource
	private PickGoodService pickGood;

	/**
	 * 已拉货|已验收|挂起
	 * 
	 * @param instock
	 * @return
	 */
	@RequestMapping(value = "/instock", method = RequestMethod.GET)
	@ResponseBody
	public BaseRes<List<OrderInstock>> instockList(String username,
			Integer oStatus) {
		logger.debug("{}",username, oStatus);
		return pickGood.pickedGood(username, oStatus);
	}

	/**
	 * 未拉货
	 * 
	 * @param instock
	 * @return
	 */
	@RequestMapping(value = "/unchecked", method = RequestMethod.GET)
	@ResponseBody
	public BaseRes<List<OrderInstock>> unCheckGood(String username) {
		logger.debug("{}",username);
		return pickGood.unPickGood(username);
	}
	
	/**
	 * 订单详情
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value="/detail",method=RequestMethod.GET)
	@ResponseBody
	public BaseRes<List<OrderInstock>> instockDetail(Long orderId){
		logger.debug("{}",orderId);
		return pickGood.instockDetail(orderId);
	}

	/**
	 * 订单详情品类列表
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value="/detailList",method=RequestMethod.GET)
	public @ResponseBody BaseRes<List<OrderInstock>> instockDetailList(Long orderId){
		logger.debug("{}",orderId);
		return pickGood.instockDetailList(orderId);
	}
	
	/**
	 * 创建拉货订单
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/create",method=RequestMethod.GET)
	public @ResponseBody Map<String,Object> createPull(String jsonData,String username){
		Gson gson = new Gson();
		Type type = new TypeToken<List<OrderInstock>>(){}.getType();
		List<OrderInstock> listInstock = gson.fromJson(jsonData, type);
		logger.debug("{}",jsonData,username);
		return pickGood.create(listInstock, username);
	}
	
	/**
	 * 补货
	 * @param jsonData
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value="/backorder",method=RequestMethod.GET)
	public @ResponseBody Map<String,Object> backOrder(String jsonData,String orderId){
		Gson gson = new Gson();
		Type type = new TypeToken<List<OrderInstock>>(){}.getType();
		List<OrderInstock> listInstock = gson.fromJson(jsonData, type);
		return pickGood.backOrder(listInstock, orderId);
	}
	
	
	/**
	 * 申请拉货
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value="/reply",method=RequestMethod.GET)
	public @ResponseBody int reply(String orderId){
		logger.debug("{}",orderId);
		return pickGood.reply(orderId);
	}
	
	/**
	 * 司机拉货
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value="/picked",method=RequestMethod.GET)
	public @ResponseBody Map<String,Object> driverPicked(String username,String password,Long orderId){
		logger.debug("{}",username,password,orderId);
		return pickGood.driverPicked(username,password,orderId);
	}
	
	/**
	 * 删除拉货订单
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.GET)
	public @ResponseBody int delete(String orderId){
		logger.debug("{}",orderId);
		return pickGood.delete(orderId);
	}
	
	/**
	 * 撤销拉货订单
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value="/repeal",method=RequestMethod.GET)
	public @ResponseBody int repeal(String orderId){
		logger.debug("{}",orderId);
		return pickGood.repeal(orderId);
	}
}
